<?php
/*
 *      c.register.class.php
 *      
 *      Copyright 2009 Valery Tverdohleb <tvsdesign@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

class ControllerRegister implements Controller
{
	protected $mode = 0;
	protected $errmes = array();
	public static function Create()
	{
		return new self;
	}
	
	public function Render()
	{
		if ($this->mode == 1)
		{
			Engine::get()->smarty->display('base.register.success.tpl');
		}
		else
		{
			Engine::get()->smarty->assign('errmes', $this->errmes);
			Engine::get()->smarty->display('base.register.form.tpl');
		}
	}
	
	public function __construct()
	{
		if (isset($_POST['m']) && ($_POST['m'] == 'register'))
		{
			$login = $_POST['login'];
			$pw = $_POST['password'];
			$name = $_POST['name'];
			$invalid = $this->_validate($login, $pw, $name);
			if ($invalid == 0)
			{
				$this->mode = 1;
				Accounting::register($login, $pw, $name);
				Engine::get()->access->login($login, $pw);
			}
			else
			{
				if ($invalid & 1)
				{
					$this->errmes[] = 'Такой логин уже занят.';
				}
				if ($invalid & 2)
				{
					$this->errmes[] = 'Логин должен быть длиной не менее 4 знака.';
				}
				if ($invalid & 4)
				{
					$this->errmes[] = 'Пароль должен быть длиной не менее 6 знаков.';
				}
				if ($invalid & 8)
				{
					$this->errmes[] = 'Игрок с таким именем уже существует.';
				}
				if ($invalid & 16)
				{
					$this->errmes[] = 'Имя должно быть длиной не менее 2 знака.';
				}
			}
		}
	}
	
	protected function _validate($login, $pw, $name)
	{
		$invalid = 0;
		//Проверка логина
		$stmt = Engine::get()->db->prepare('SELECT COUNT(*) FROM `users` WHERE `login` = :login;');
		$stmt->bindValue(':login', $login);
		$stmt->execute();
		if ($stmt->fetchColumn() != 0)
		{
			$invalid |= 1;
		}
		if (mb_strlen($login, 'utf8') < 4)
		{
			$invalid |= 2;
		}
		//Проверка пароля
		if (mb_strlen($pw, 'utf8') < 6)
		{
			$invalid |= 4;
		}
		//Проверка имени игрока
		$stmt = Engine::get()->db->prepare('SELECT COUNT(*) FROM `users` WHERE `name` = :name;');
		$stmt->bindValue(':name', $name);
		$stmt->execute();
		if ($stmt->fetchColumn() != 0)
		{
			$invalid |= 8;
		}
		if (mb_strlen($name, 'utf8') < 2)
		{
			$invalid |= 16;
		}
		return $invalid;
	}
	
}

?>
